package com.tarzan.edu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.edu.entity.EduSubject;
import com.tarzan.edu.entity.request.EduSubjectReq;
import com.tarzan.edu.entity.response.EduSubjectResp;
import com.tarzan.edu.entity.response.TreeEduSubject;
import com.tarzan.edu.service.IEduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @description TODO 课程分类管理
 * @author liujinshan
 * @data 2021/6/18 15:51
 */
@RestController
@RequestMapping("edu/subject")
public class EduSubjectController {

    @Autowired
    private IEduSubjectService iEduSubjectService;



    /** @title insert
     * @description TODO 新增课程分类信息
     * @param eduSubject
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:52
     */
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse insert(@RequestBody EduSubject eduSubject){
        try{
            iEduSubjectService.insert(eduSubject);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @title findTreeEduSubject
     * @description TODO 查询课程分类树型结果
     * @param
     * @return com.tarzan.common.core.base.BasePaginationResponse<com.tarzan.edu.entity.response.TreeEduSubject>
     * @author liujinshan
     * @data 2021/6/21 16:02
     */
    @GetMapping("findTreeEduSubject")
    public BaseResponse<List<TreeEduSubject>> findTreeEduSubject(){
        try{
            return BaseResponse.ok(iEduSubjectService.findTreeEduSubject());
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
    }

    /** @title update
     * @description TODO 修改课程分类信息
     * @param eduSubject
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:59
     */
    @PutMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody EduSubject eduSubject){
        try{
            iEduSubjectService.update(eduSubject);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * @title delete
     * @description TODO 删除课程分类信息
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse
     * @author liujinshan
     * @data 2021/6/16 13:15
     */
    @DeleteMapping(value = "delete/{id}")
    public BaseResponse delete(@PathVariable String id){
        try{
            iEduSubjectService.delete(id);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @description 获取课程分类信息列表并分页
     * @author liujinshan
     * param eduTeacherReq
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @date 2021-5-23 14:43
     */
    @GetMapping(value = "findSubjectPageInfo")
    public BaseResponse<IPage<EduSubjectResp>> findSubjectPageInfo(@RequestBody EduSubjectReq eduSubjectReq){
        BaseResponse<IPage<EduSubjectResp>> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iEduSubjectService.findSubjectPageInfo(eduSubjectReq));
        } catch (BusinessException e){
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
        } catch (Exception e){
            response.setErrCode("500");
            response.setErrMsg(e.getMessage());
        }
        return response;
    }

    /**
     * @title findCourseById
     * @description TODO 根据id查询课程分类信息相细信息
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse<com.tarzan.edu.entity.response.EduSubjectResp>
     * @throws 
     * @author liujinshan
     * @data 2021/6/16 11:49
     */
    @GetMapping(value = "findTeacherById/{id}")
    public BaseResponse<EduSubjectResp> findCourseById(@PathVariable String id){
        BaseResponse<EduSubjectResp> response;
        try{
            response = BaseResponse.ok(iEduSubjectService.findSubjectById(id));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }

}
